package com.sdry.web.controller.zc.quality;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sdry.model.lz.ActualTimeInfo;
import com.sdry.model.lz.LzQueryCriteria;
import com.sdry.model.workplace.MousseSample;
import com.sdry.model.lz.ProcessItem;
import com.sdry.service.lz.ProcessItemService;
import com.sdry.utils.ResponseUtil;

/**
 * 
 * @ClassName ProcessItemController
 * @Description 工艺巡检项
 * @Author lz
 * @Date 2018年11月2日 11:16:22
 * @Version 1.0
 */
@Controller
@RequestMapping("/processItem")
public class ProcessItemController {
	
	public static final Integer AFFECT_ROW = 0;
	
	@Resource
	ProcessItemService processItemService;
	
	/**
     * 增加工艺巡检记录
     * @param response
     * @param processItem
     * @throws Exception
     */
    @RequestMapping("/addProcessItem")
    public void addProcessItem(HttpServletResponse response, ProcessItem processItem) throws Exception {
    	/*Long affectRow = 0L;
    	if (StringUtil.isNotEmpty(processItem.getFlag())) {
			if ("1".equals(processItem.getFlag())) {
				affectRow = processItemService.addProcessItem(processItem);
			} else {
				affectRow = processItemService.addProcessItem(processItem);
			}
		}*/
    	Long affectRow= processItemService.addProcessItem(processItem);
        if (affectRow > AFFECT_ROW) {
            ResponseUtil.write(response, affectRow);
        } else {
            ResponseUtil.write(response, AFFECT_ROW);
        }
    }

    /**
     * 单个删除和批量删除
     * @param response
     * @param idArr id数组
     * @throws Exception
     */
    @RequestMapping("/deleteProcessItemById")
    public void deleteProcessItemById(HttpServletResponse response, @RequestParam("idArr[]") String[] idArr) throws Exception {
        for (int i = 0; i < idArr.length; i++) {
            Long affectRow = processItemService.deleteProcessItemById(Long.parseLong(idArr[i]));
            if (affectRow > AFFECT_ROW) {
                ResponseUtil.write(response, affectRow);
            } else {
                ResponseUtil.write(response, AFFECT_ROW);
            }
        }
    }
    
    /**
     * 根据ID修改巡检项信息
     * @param response
     * @param processItem
     * @throws Exception
     */
    @RequestMapping("/editProcessItemById")
    public void editProcessItemById(HttpServletResponse response, ProcessItem processItem) throws Exception {
        Long affectRow = processItemService.editProcessItemById(processItem);
        if (affectRow > AFFECT_ROW) {
            ResponseUtil.write(response, affectRow);
        } else {
        	ResponseUtil.write(response, AFFECT_ROW);
        }
    }
    
    /**
     * 分页查询巡检项
     * @param response
     * @param criteria 分页查询条件
     */
    @RequestMapping("/queryProcessItem")
    public void queryProcessItem(HttpServletResponse response, LzQueryCriteria criteria){
        String keyword01 = criteria.getKeyword01();
        String keyword02 = criteria.getKeyword02();
        if(keyword01 == null){
            keyword01 = "";
        }
        if(keyword02 == null){
            keyword02 = "";
        }
        criteria.setKeyword01(keyword01);
        criteria.setKeyword02(keyword02);
        List<ProcessItem> processItemList = processItemService.queryProcessItem(criteria);
        int count = processItemService.countProcessItem(criteria);
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(processItemList);
        jsonObject.put("code", AFFECT_ROW);
        jsonObject.put("data", jsonArray);
        jsonObject.put("count", count);
        try {
            ResponseUtil.write(response, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 查询组件浇铸巡检项
     * @throws Exception 
     */
    @RequestMapping("/queryProcessItemFlag1")
    public void queryProcessItemFlag1(HttpServletResponse response) throws Exception{
    	List<ProcessItem> processItemList = processItemService.queryProcessItemFlag1();
    	JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(processItemList);
    	if (processItemList.size() > AFFECT_ROW) {
            jsonObject.put("code", AFFECT_ROW);
            jsonObject.put("data", jsonArray);
            jsonObject.put("count", 10);
            ResponseUtil.write(response, jsonObject);
		} else {
			jsonObject.put("code", AFFECT_ROW);
            jsonObject.put("data", null);
            jsonObject.put("count", 10);
            ResponseUtil.write(response, jsonObject);
		}
    }
    
    /**
     * 查询纺丝车间巡检项
     * @throws Exception 
     */
    @RequestMapping("/queryProcessItemFlag2")
    public void queryProcessItemFlag2(HttpServletResponse response) throws Exception{
    	List<ProcessItem> processItemList = processItemService.queryProcessItemFlag2();
    	JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(processItemList);
    	if (processItemList.size() > AFFECT_ROW) {
            jsonObject.put("code", AFFECT_ROW);
            jsonObject.put("data", jsonArray);
            jsonObject.put("count", 10);
            ResponseUtil.write(response, jsonObject);
		} else {
			jsonObject.put("code", AFFECT_ROW);
            jsonObject.put("data", null);
            jsonObject.put("count", 10);
            ResponseUtil.write(response, jsonObject);
		}
    }
    
    /**
     * 离心机和纺丝车间实时信息登记
     * @param response
     * @param actualTimeInfo 实时信息实体
     * @throws Exception 
     */
    @RequestMapping("/addActualTimeInfo")
    public void addActualTimeInfo(HttpServletResponse response, ActualTimeInfo actualTimeInfo) throws Exception {
    	if ("21".equals(actualTimeInfo.getD_s_id())) {
    		actualTimeInfo.setD_s_id("1#热法纺丝线");
		} else if ("22".equals(actualTimeInfo.getD_s_id())) {
			actualTimeInfo.setD_s_id("2#热法纺丝线");
		} else if ("23".equals(actualTimeInfo.getD_s_id())) {
            actualTimeInfo.setD_s_id("3#热法纺丝线");
        } else if ("24".equals(actualTimeInfo.getD_s_id())) {
            actualTimeInfo.setD_s_id("4#热法纺丝线");
        } else if ("31".equals(actualTimeInfo.getD_s_id())) {
            actualTimeInfo.setD_s_id("1#湿法纺丝线");
        } else if ("32".equals(actualTimeInfo.getD_s_id())) {
            actualTimeInfo.setD_s_id("2#湿法纺丝线");
        } else {
			actualTimeInfo.setD_s_id(actualTimeInfo.getD_s_id() + "号离心机");
		}
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    	actualTimeInfo.setCheckTime(sdf.format(new Date()));
    	Long affectRow= processItemService.addActualTimeInfo(actualTimeInfo);
        if (affectRow > AFFECT_ROW) {
            ResponseUtil.write(response, affectRow);
        } else {
            ResponseUtil.write(response, AFFECT_ROW);
        }
    }
    
    /**
     * 分页查询纺丝车间和组件浇铸实时信息实体
     * @param response
     * @param criteria
     */
    @RequestMapping("/queryActualTimeInfoList")
    public void queryActualTimeInfoList(HttpServletResponse response, LzQueryCriteria criteria){
        String keyword01 = criteria.getKeyword01();
        String keyword02 = criteria.getKeyword02();
        if(keyword01 == null){
            keyword01 = "";
        }
        if(keyword02 == null){
            keyword02 = "";
        }
        criteria.setKeyword01(keyword01);
        criteria.setKeyword02(keyword02);
        List<ActualTimeInfo> ActualTimeInfoList = processItemService.queryActualTimeInfoList(criteria);
        int count = processItemService.countActualTimeInfoList(criteria);
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(ActualTimeInfoList);
        jsonObject.put("code", AFFECT_ROW);
        jsonObject.put("data", jsonArray);
        jsonObject.put("count", count);
        try {
            ResponseUtil.write(response, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 分页查询膜丝抽检列表
     * @param response
     * @param criteria
     */
    @RequestMapping("/queryMembraneFilaments")
    public void queryMembraneFilaments(HttpServletResponse response, LzQueryCriteria criteria){
        String keyword01 = criteria.getKeyword01();
        String keyword02 = criteria.getKeyword02();
        if(keyword01 == null){
            keyword01 = "";
        }
        if(keyword02 == null){
            keyword02 = "";
        }
        criteria.setKeyword01(keyword01);
        criteria.setKeyword02(keyword02);
        List<MousseSample> mousseSampleList = processItemService.queryMembraneFilaments(criteria);
        int count = processItemService.countMembraneFilaments(criteria);
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(mousseSampleList);
        jsonObject.put("code", AFFECT_ROW);
        jsonObject.put("data", jsonArray);
        jsonObject.put("count", count);
        try {
            ResponseUtil.write(response, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
}
